package com.block.product.service.union;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.crypto.SecureUtil;
import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.block.common.constant.AppRspCodeConstant;
import com.block.common.service.LogFactory;
import com.block.common.vo.ResponseVo;
import com.block.db.common.vo.BaseUserInfoVo;
import com.block.db.entity.ProductInfo;
import com.block.product.service.IUnionProductService;
import org.slf4j.Logger;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

/**
 * 叮叮帮 联登  业务
 */
@Service
public class DDBProductServiceImpl implements IUnionProductService {

    private static Logger log = LogFactory.getCommLog();

    private static final String MATCH_USER_URL = "https://ddb.gujishuke.com/ddb/external/check/user";
//    private static final String MATCH_USER_URL = "https://ddb-test.gujishuke.com/ddb/external/check/user";

    private static final String REGISTER_USER_URL = "https://ddb.gujishuke.com/ddb/external/union/register";
//    private static final String REGISTER_USER_URL = "https://ddb-test.gujishuke.com/ddb/external/union/register";

    @Override
    public ResponseVo matchUserInfo(ProductInfo productInfo, BaseUserInfoVo userInfoVo) {

        try {

            HttpRequest httpRequest = HttpUtil.createPost(MATCH_USER_URL);
            httpRequest.header("Content-Type", "application/json");

            String md5Mobile = SecureUtil.md5(userInfoVo.getMobile()).toUpperCase();
            String timestamp = String.valueOf(System.currentTimeMillis());

            String sign = productInfo.getChannelCode() + md5Mobile + timestamp + productInfo.getChannelSecret();
            String md5Sign = SecureUtil.md5(sign).toUpperCase();

            Map<String, Object> paramMap = new HashMap<>();
            paramMap.put("appType", productInfo.getChannelCode());
            paramMap.put("mobileMd5", md5Mobile);
            paramMap.put("deviceType", "android");
//            paramMap.put("deviceBrand", md5Mobile);
//            paramMap.put("deviceVersion", md5Mobile);
//            paramMap.put("deviceModel", md5Mobile);
            paramMap.put("channel", productInfo.getChannelKey());
            paramMap.put("timeStamp", timestamp);
            paramMap.put("sign", md5Sign);

//            httpRequest.form(JSON.toJSONString(paramMap));
            httpRequest.body(JSON.toJSONString(paramMap));
            String resultStr = httpRequest.execute().body();
            log.info("【产品联登】【叮叮帮】撞库完成【resultStr】{}【MATCH_USER_URL】{}【paramMap】{}", resultStr, MATCH_USER_URL, JSON.toJSONString(paramMap));

            JSONObject jsonObject = JSON.parseObject(resultStr);
            Integer result = jsonObject.getInteger("result");
            boolean success = jsonObject.getBoolean("success");
            if (!ObjectUtil.equals(Integer.valueOf("1"), result) || !success){
                return new ResponseVo(AppRspCodeConstant.THRID_FAILED);
            }

            return new ResponseVo(AppRspCodeConstant.SUCCESS);
        }catch (Exception exception){
            log.error("【产品联登】【叮叮帮】撞库异常【userInfoVo】" + JSON.toJSONString(userInfoVo), exception);
            return new ResponseVo(AppRspCodeConstant.SYS_EXCEPTION);
        }
    }

    @Override
    public ResponseVo registerUserInfo(ProductInfo productInfo, BaseUserInfoVo userInfoVo) {

        try {

            HttpRequest httpRequest = HttpUtil.createPost(REGISTER_USER_URL);
            httpRequest.header("Content-Type", "application/json");

            String timestamp = String.valueOf(System.currentTimeMillis());

            String sign = productInfo.getChannelCode() + userInfoVo.getMobile() + timestamp + productInfo.getChannelSecret();
            String md5Sign = SecureUtil.md5(sign).toUpperCase();

            Map<String, Object> paramMap = new HashMap<>();
            paramMap.put("appType", productInfo.getChannelCode());
            paramMap.put("mobile", userInfoVo.getMobile());
            paramMap.put("deviceType", "android");
            paramMap.put("channel", productInfo.getChannelKey());
            paramMap.put("timeStamp", timestamp);
            paramMap.put("sign", md5Sign);

            httpRequest.body(JSON.toJSONString(paramMap));
            String resultStr = httpRequest.execute().body();
            log.info("【产品联登】【叮叮帮】注册完成【result】{}【MATCH_USER_URL】{}【paramMap】{}", resultStr, REGISTER_USER_URL, JSON.toJSONString(paramMap));

            JSONObject jsonObject = JSON.parseObject(resultStr);
            Integer result = jsonObject.getInteger("result");
            boolean success = jsonObject.getBoolean("success");
            if (!ObjectUtil.equals(Integer.valueOf("1"), result) || !success){
                return new ResponseVo(AppRspCodeConstant.THRID_FAILED, resultStr, "");
            }

            String downloadUrl = jsonObject.getString("downloadUrl");
            JSONObject resultData=  new JSONObject();
            resultData.put("productUrl", downloadUrl);

            return new ResponseVo(AppRspCodeConstant.SUCCESS, resultData);
        }catch (Exception exception){
            log.error("【产品联登】【叮叮帮】注册异常【userInfoVo】" + JSON.toJSONString(userInfoVo), exception);
            return new ResponseVo(AppRspCodeConstant.SYS_EXCEPTION);
        }
    }

    public static void main(String[] args) {
        System.out.println(System.currentTimeMillis());
    }

}
